package demo;

public class ThreadLocalDemo {
  private static ThreadLocal<Integer> localNumber = new ThreadLocal<>();
  private static ThreadLocal<String> localString = new ThreadLocal<>();

  public static void main(String[] args) throws InterruptedException {
    var threadName = Thread.currentThread().getName();

    var thread01 = new Thread(new Task01());
    var thread02 = new Thread(new Task02());

    thread01.start();
    thread02.start();

    Thread.sleep(2L);

    System.out.println("[Main] - " + threadName + " Integer value is "+ localNumber.get());
    System.out.println("[Main] - " + threadName + " String value is "+ localString.get());
  }

  private static class Task01 implements Runnable{

    @Override
    public void run() {
      var threadName = Thread.currentThread().getName();

      System.out.println("[Task01] Begin - " + threadName + " Integer value is "+ localNumber.get());
      System.out.println("[Task01] Begin - " + threadName + " String value is "+ localString.get());

      localNumber.set(3001);
      localString.set("[Task01] - Hello");

      
      System.out.println("[Task01] Finished - " + threadName + " Integer value is "+ localNumber.get());
      System.out.println("[Task01] Finished - " + threadName + " String value is "+ localString.get());
    }
  }

  private static class Task02 implements Runnable{

    @Override
    public void run() {
      var threadName = Thread.currentThread().getName();

      System.out.println("[Task02] Begin - " + threadName + " Integer value is "+ localNumber.get());
      System.out.println("[Task02] Begin - " + threadName + " String value is "+ localString.get());

      localNumber.set(8001);
      localString.set("[Task02] - World");

      
      System.out.println("[Task02] Finished - " + threadName + " Integer value is "+ localNumber.get());
      System.out.println("[Task02] Finished - " + threadName + " String value is "+ localString.get());
    }

  }
}
